package com.zjy.software.day16;

import java.io.*;

public class FileReaderWriterDemo {
    public static void main(String[] args) throws IOException {
//        testFileReader();
//        testFileWrite();
//        testInputStreamReader();
//        testBufferedReader();
        testBufferedWriter();
    }

    private static void testBufferedWriter() throws IOException {
        FileWriter fileWriter = new FileWriter("buffer.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write("hello world 我是兰州人");
        bufferedWriter.newLine();
        bufferedWriter.write("  欢迎来兰州  我是兰州人");

        bufferedWriter.close();
    }

    private static void testBufferedReader() throws IOException {
        FileReader fileReader = new FileReader("newfile.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String s = bufferedReader.readLine();
        System.out.println("s = " + s);
        bufferedReader.close();

    }

    private static void testInputStreamReader() throws IOException {
        InputStream inputStream = new FileInputStream("newfile.txt");

        //定义转化流,把原来的字节流转化为字符流,可以使用字符流方法,构造器里面传入的是字节流,也是原始流
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        char[] chars = new char[40];
        inputStreamReader.read(chars);
        String s = new String(chars);
        System.out.println(s);
        inputStreamReader.close();
    }

    private static void testFileReader() throws IOException {
        File file = new File("newfile.txt");
        FileReader fileReader = new FileReader(file);
        int read = fileReader.read();
        char[] chars = new char[10];
        fileReader.read(chars);
        String s = new String(chars);
        System.out.println("s = " + s);
        fileReader.close();
    }
    private static void testFileWrite() throws IOException {
        File file = new File("newfile.txt");
        FileWriter fileWriter = new FileWriter(file);

        fileWriter.write("hello world  我是兰州人");


        fileWriter.close();
    }

}